# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=moab
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.3.1
pkgrel=3
pkgdesc='The Mesh-Oriented datABase MOAB is a component for representing and evaluating mesh data (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
license=('spdx:LGPL-3.0+')
url='https://sigma.mcs.anl.gov/moab-library/'
source=(https://ftp.mcs.anl.gov/pub/fathom/${_realname}-${pkgver}.tar.gz
        "0001-no-fortran.patch")
sha256sums=('2404fab2d84f87be72b57cfef5ea237bfa444aaca059e66a158f22134956fe54'
            '2669507edebea057297ef7fb4e4ced4e5bc2ac350da78b24ac51a7bcb3559a94')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-no-fortran.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _extra_cmake_flags+=" -DENABLE_FORTRAN=OFF"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DENABLE_MPI=ON \
      -DENABLE_TESTING=ON \
      ${_extra_cmake_flags} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
